import "core-js/stable";
import "regenerator-runtime/runtime";

import "./public-path";

import Vue from "vue";
import App from "./App.vue";
import router from "./router";
import store from "./store";
import ViewUI from "view-design";
// import echarts from "vue-echarts-v3/src/full.js";
import "view-design/dist/styles/iview.css";

import BaiduMap from "vue-baidu-map";

Vue.use(BaiduMap, {
  // ak 是在百度地图开发者平台申请的密钥 详见 http://lbsyun.baidu.com/apiconsole/key */
  ak: "UjhyqCljlxVqzDQ8LzMuDEjYp0iOE90G"
});

Vue.use(ViewUI);
// Vue.component("echarts", echarts);

Vue.config.productionTip = false;

let instance = null;
async function render(props = {}) {
  let userInfo = localStorage.getItem("userInfo");
  console.log("userInfo", userInfo);
  await import("@/components");

  const { container } = props;

  instance = new Vue({
    router,
    store,
    render: h => h(App)
  }).$mount(container ? container.querySelector("#app") : "#app");
}

if (!window.__POWERED_BY_QIANKUN__) {
  render();
}

export async function bootstrap() {}

export async function mount(props) {
  store.commit("props", props);

  props.onGlobalStateChange(value => {
    if (value.mapConfig) {
      store.commit("MAP_INIT_CONFIG", value.mapConfig);
    }
  }, true);
  props.setGlobalState({
    loading: false
  });
  render(props);
}

export async function unmount() {
  if (instance) {
    instance.$destroy();
  }
  instance = null;
}

export async function update(props) {
  console.log("update props", props);
}

router.beforeEach((to, from, next) => {
  console.log(from);
  if (to.matched[0]) {
    next();
  }
});
